package com.huike.web.controller.clues;


import com.huike.clues.domain.TbCourse;
import com.huike.clues.domain.dto.TbCourseDTO;
import com.huike.clues.service.TbCourseService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.bean.BeanUtils;
import com.huike.log.logOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 课程管理Controller
 * @date 2023-04-01
 */
@RestController
@RequestMapping("/clues/course")
public class TbCourseController extends BaseController {
    @Autowired
    TbCourseService tbCourseService;

    @GetMapping("/list")
    public TableDataInfo list(TbCourseDTO dto) {
        startPage();
        List<TbCourse> list = tbCourseService.lambdaQuery()
                .like(dto.getCode() != null, TbCourse::getCode, dto.getCode())
                .like(dto.getApplicablePerson() != null, TbCourse::getApplicablePerson, dto.getApplicablePerson())
                .like(dto.getSubject() != null, TbCourse::getSubject, dto.getSubject())
                .list();
        return getDataTable(list);
    }


    /**
     * 新增课程
     * @return
     */
    @PostMapping
    @logOperation(title = "新增课程", businessType = 1)

    public AjaxResult add(@RequestBody TbCourseDTO dto) {
        String uuid = UUID.randomUUID().toString().substring(0, 8);
        dto.setCode(uuid);
        dto.setCreateTime(new Date());
        TbCourse tbCourse = new TbCourse();
        BeanUtils.copyProperties(dto, tbCourse);
        tbCourseService.save(tbCourse);

        return success();
    }

    /**
     * 查询回显
     */
    @GetMapping("/{id}")
    public AjaxResult listbyid(@PathVariable Integer id) {
        TbCourse byId = tbCourseService.getById(id);
        return AjaxResult.success(byId);
    }

    @logOperation(title = "修改课程", businessType = 2)
    /**
     *修改
     */
    @PutMapping
    public AjaxResult update(@RequestBody TbCourseDTO dto) {
        TbCourse tbCourse = new TbCourse();
        BeanUtils.copyProperties(dto, tbCourse);
        tbCourseService.updateById(tbCourse);
        return success();
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @logOperation(title = "删除课程", businessType = 3)
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable String ids) {
        tbCourseService.removeById(ids);
        return success();
    }

    /**
     * 课程下拉列表
     */
    @GetMapping("/listselect")
    public AjaxResult listselect(String subject) {
        List<TbCourse> list = tbCourseService.lambdaQuery()
                .like(subject != null, TbCourse::getSubject, subject)
                .list();
        return AjaxResult.success(list);
    }
}
